import axios from 'axios';
import {
  Message
} from 'element-ui'
import router from '../router'

const http = axios.create({
  baseURL: process.env.BASE_URL
})

// 请求拦截
http.interceptors.request.use(config => {
  if (localStorage.token) {
    config.headers.Authorization = localStorage.token
  }
  return config
})
// 响应拦截
http.interceptors.response.use(res => {
  if (res.data.status == 102 || res.data.status == 103) {
    Message(res.data.msg)
    localStorage.removeItem('token')
    localStorage.removeItem('userName')
    localStorage.removeItem('rule')
    router.push('/login')
  } else {
    return res
  }
}, err => {
  Message('未知错误')
  console.log(err)
})

export default http
